package com.example.evanproject.Interceptor;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.example.evanproject.domain.Register;
import com.example.evanproject.domain.User;
import com.example.evanproject.mapper.RegisterMapper;
import com.example.evanproject.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static java.lang.Character.getType;

@CrossOrigin
public class JwtInterceptor  implements HandlerInterceptor {
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {

        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            return true;
        }

        String token = request.getHeader("token");
        String roldid = request.getHeader("roldid");
//        System.out.println(request.getRequestURI());
        if (StrUtil.isBlank(token)) {
            token = request.getParameter("token");
            roldid = request.getParameter("roldid");
        }

        // 执行认证
        if (StrUtil.isBlank(token)) {
            response.setStatus(401);
            return false;
        }
        // 获取 token 中的 user id
        String userId;
        try {
            userId = JWT.decode(token).getAudience().get(0);
        } catch (Exception e) {
            System.out.println("失败");
            response.setStatus(401);
            return false;
        }


        if (roldid.equals("2")||Integer.valueOf(roldid)==2) {
//            按照系统用户表查询的信息表中查询
            // 根据token中的userid查询数据库
            User userlist = userMapper.selectinfo(Integer.valueOf(userId));
            if (userlist == null) {
                response.setStatus(401);
                return false;
            }
            // 用户密码加签验证 token(使用用户密码加密的方式 创建一个加密规则)
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(userlist.getUserpassword())).build();
            try {
//            使用密码创建出出来的加密规则来验证是否合法
                jwtVerifier.verify(token); // 验证token
            } catch (JWTVerificationException e) {
                response.setStatus(401);
                return false;
            }

            return true;
        }

        if(roldid.equals("1")||Integer.valueOf(roldid)==1)
        {
//        按照注册表的信息查询
        // 根据token中的userid查询数据库
        Register user = registerMapper.selectById(Integer.valueOf(userId));
         if (user == null) {
            response.setStatus(401);
            return false;
        }
        // 用户密码加签验证 token(使用用户密码加密的方式 创建一个加密规则)
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getSPassword())).build();
        try {
//            使用密码创建出出来的加密规则来验证是否合法
            jwtVerifier.verify(token); // 验证token
        } catch (JWTVerificationException e) {
            response.setStatus(401);
            return false;
        }
      return true;
        }
        return true;
    }
}
